package com.cf.core.player.impl;

import com.cf.core.player.Player;
import com.cf.core.player.UserInfo;
import com.cf.compent.location.City;
import com.cf.game.service.util.BeanFactoryUtil;
import com.cf.game.service.util.ObjectJsonUtil;
import com.cf.game.service.util.Reflections;
import com.cf.game.service.util.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.tomcat.util.net.NioChannel;
import org.apache.tomcat.util.net.NioEndpoint;
import org.apache.tomcat.websocket.server.WsRemoteEndpointImplServer;

import javax.websocket.Session;
import java.net.Inet4Address;
import java.net.InetSocketAddress;

/**
 * game-server-DefaultPlayer
 *
 * @author <a href="2428922347@qq.com">Zhu mingming</a>
 * @info
 * @since 2018/5/7 下午 03:46
 */
@Slf4j
public class DefaultWebSocketPlayer implements Player {

    private Session session;

    private UserInfo userInfo;


    public DefaultWebSocketPlayer(Session session, UserInfo userInfo) {
        this.session = session;
        this.userInfo = userInfo;
    }

    @Override
    public Integer getId() {
        return userInfo.getUserId();
    }

    @Override
    public String getName() {
        return userInfo.getName();
    }

    @Override
    public String getHead() {
        return userInfo.getHead();
    }

    @Override
    public String getToken() {
        return userInfo.getToken();
    }

    @Override
    public void sendMessage(String message) {
        synchronized (session) {
            try {
                if (session.isOpen()) {
                    session.getBasicRemote().sendText(message);
                }
            } catch (Exception e) {
                log.error("发送数据失败！", e);
            }
        }
    }

    @Override
    public void sendMessage(Object message) {
        synchronized (session) {
            if (message == null)
                return;
            try {
                if (session.isOpen()) {
                    String sendMessage = ObjectJsonUtil.toString(message);
                    session.getBasicRemote().sendText(sendMessage);
                }
            } catch (Exception e) {
                log.error("发送数据失败！", e);
            }
        }
    }

    @Override
    public String getLocation() {
        try {
            String[] strings = BeanFactoryUtil.getBean(City.class).find(getIp());
            if (strings.length >= 3)
                return StringUtils.hasLength(strings[2]) ? strings[2] : "内网用户";
        } catch (Exception e) {
            return "上海";
        }
        return "上海";
    }

    @Override
    public void clean() {
        this.session = null;
    }

    private String getIp() {
        try {
            WsRemoteEndpointImplServer wsRemoteEndpoint = (WsRemoteEndpointImplServer) Reflections.getFieldValue(session, "wsRemoteEndpoint");
            NioEndpoint.NioSocketWrapper socketWrapper = (NioEndpoint.NioSocketWrapper) Reflections.getFieldValue(wsRemoteEndpoint, "socketWrapper");
            InetSocketAddress remoteAddress = (InetSocketAddress) ((NioChannel) socketWrapper.getSocket()).getIOChannel().getRemoteAddress();
            return ((Inet4Address) remoteAddress.getAddress()).getHostAddress();
        } catch (Exception e) {
            return "120.55.189.166";
        }
    }


}
